package com.longkey.workflow.client.property
{
	import com.longkey.workflow.client.event.PropertyEvent;
	import com.longkey.workflow.client.util.IDGenerator;
	
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayList;
	import mx.collections.IList;
	
	import spark.components.SkinnableContainer;
	
	public class PropertyObject extends SkinnableContainer
	{
		
		private var _title:String;
		private var _value:String;
		private var _label:String;
		private var _type:int;
		private var _dataProvider:IList;
		private var _selectIndex:int;
		private var _parentEditor:PropertyEditor;
		
		public function PropertyObject(parent:PropertyEditor)
		{
			super();
			this._value = null;
			this._label = "";
			this._selectIndex = -1;
			this._parentEditor = parent;
			this.height = 32;
			this.percentWidth = 100;
			this.setStyle("skinClass", com.longkey.workflow.client.property.PropertySkin);
//			this.addEventListener(MouseEvent.CLICK, onClick);
		}
		
		public function get parentEditor():PropertyEditor{
			return this._parentEditor;
		}
		
		public function unselectOthers():void{
			for(var k:String in parentEditor.props){
				if(k == this.id)
					continue;
				var prop:PropertyObject = parentEditor.props[k] as PropertyObject;
				prop.skin.currentState = "normal";
			}
		}
		
		public function setValue(value:String = null, label:String = null, idx:int = -1):void{
			switch(this._type){
				case PropertySelector.TEXT:
				case PropertySelector.TEXTAREA:
				case PropertySelector.MESSAGEEDITOR:
				case PropertySelector.UUIDSELECTOR:
					this.value = value;
					this.label = value;
					break;
				case PropertySelector.USERSELECTOR:
				case PropertySelector.ROLESELECTOR:
				case PropertySelector.DEPTSELECTOR:
				case PropertySelector.FORMITEMSELECTOR:
				case PropertySelector.FORMSELECTOR:
				case PropertySelector.SITESELECTOR:
					this.value = value;
					this.label = label;
					break;
				case PropertySelector.LIST:
					if(idx >= 0 && this.dataProvider.length > idx){
						var item:Object = this.dataProvider.getItemAt(idx);
						this.value = item["value"];
						this.label = item["label"];
						this.selectedIndex = idx;
					}
					else if(value != null && value != "" && this.dataProvider.length > 0){
						for(var i:int = 0; i < this.dataProvider.length; i++){
							var one:Object = this.dataProvider.getItemAt(i);
							if(one.hasOwnProperty("value") && one["value"] == value){
								this.value = value;
								this.label = one["label"];
								this.selectedIndex = i;
								break;
							}
						}
					}
					break;
				default:
					break;
			}
		}
		
		public function set value(value:String):void{
			this._value = value;
		}
		
		public function get value():String{
			return this._value;
		}
		
		[Bindable]
		public function set title(tit:String):void{
			this._title = tit;
		}
		
		public function get title():String{
			return this._title;
		}
		
		[Bindable]
		public function set label(label:String):void{
			this._label = label;
		}
		
		public function get label():String{
			return this._label;
		}
		
		public function set type(type:int):void{
			this._type = type;
		}
		
		public function get type():int{
			return this._type;
		}
		
		public function setDataProvider(data:Object):void{
			if(data != null){
				if(data is IList){
					this._dataProvider = IList(data);
				}
				else if(data is Array){
					this._dataProvider = new ArrayList(data as Array);
				}
			}
		}
		
		[Bindable]
		public function set dataProvider(data:IList):void{
			this._dataProvider = data;
		}
		
		public function get dataProvider():IList{
			if(this._dataProvider == null)
				return new ArrayList();
			return this._dataProvider;
		}
		
		[Bindable]
		public function set selectedIndex(idx:int):void{
			this._selectIndex = idx;
		}
		
		public function get selectedIndex():int{
			return this._selectIndex;
		}
	}
}